package com.fang.consumer.config;

import java.nio.charset.Charset;
import java.util.Base64;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {

	/*
	 * 消费者进行解密的过程中，需要进行一个Http头信息配置
	 */
	@Bean
	public HttpHeaders getHeaders() {
		HttpHeaders header = new HttpHeaders();
		String auth = "fang@123456";
		// 加密处理
		byte[] encodeAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));
		String authHeader = "Basic " + new String(encodeAuth);
		header.set("Authorization", authHeader);
		return header;
	}

	/*
	 * @LoadBalanced是Ribbon的配置 
	 * 自己的配置类，放在SpringCloud找不到的地方。
	 */
	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate() {
		return new RestTemplate();
	}
}
